import java.awt.Color;
import java.awt.Graphics;

import Primitives.Point;

public class StickFigure {

	Gun gun = new Gun();

	double xPos, yPos;

	double direction;

	double xVel, yVel;

	int xDir, yDir;

	int moveSpeed = 2;

	int radius = 8;

	boolean moveForward, moveBackward, moveLeft, moveRight, firing;

	public void draw(Map gameMap, Graphics g) {

		xDir = (int) (xPos + radius * 1.33
				* Math.cos((direction % 360) / 360.0 * Math.PI * 2));
		yDir = (int) (yPos + radius * 1.33
				* Math.sin((direction % 360) / 360.0 * Math.PI * 2));

		g.setColor(Color.white);
		gameMap.fillOval(g, (int) xPos - radius, (int) yPos - radius,
				radius * 2, radius * 2);
		g.setColor(Color.red);
		gameMap.fillOval(g, xDir - radius / 3, yDir - radius / 3,
				2 * radius / 3, 2 * radius / 3);

	}

	public void physics() {

		xVel = 0;
		yVel = 0;

		double xDir, yDir;

		boolean move = false;

		if (moveForward) {

			move = true;

			xDir = Math.cos((direction % 360) / 360.0 * Math.PI * 2);
			yDir = Math.sin((direction % 360) / 360.0 * Math.PI * 2);

			xVel += xDir;
			yVel += yDir;

		}

		if (moveBackward) {

			move = true;

			xDir = Math.cos((direction % 360) / 360.0 * Math.PI * 2);
			yDir = Math.sin((direction % 360) / 360.0 * Math.PI * 2);

			xVel -= xDir;
			yVel -= yDir;

		}
		if (moveLeft) {

			move = true;

			xDir = Math.cos(((direction - 90) % 360) / 360.0 * Math.PI * 2);
			yDir = Math.sin(((direction - 90) % 360) / 360.0 * Math.PI * 2);

			xVel += xDir;
			yVel += yDir;

		}
		if (moveRight) {

			move = true;

			xDir = Math.cos(((direction + 90) % 360) / 360.0 * Math.PI * 2);
			yDir = Math.sin(((direction + 90) % 360) / 360.0 * Math.PI * 2);

			xVel += xDir;
			yVel += yDir;

		}

		if (!move) {
			return;
		}

		gun.recoil += gun.movementRecoil;

		double dist = Math.pow((xVel) * (xVel) + (yVel) * (yVel), 0.5);

		if (dist < 0.01) {
			return;
		}

		xVel = xVel * 2 / dist;
		yVel = yVel * 2 / dist;

		xPos += xVel;
		yPos += yVel;
	}

	public Point getNextPoint() {

		xVel = 0;
		yVel = 0;

		double xDir, yDir;

		boolean move = false;

		if (moveForward) {

			move = true;

			xDir = Math.cos((direction % 360) / 360.0 * Math.PI * 2);
			yDir = Math.sin((direction % 360) / 360.0 * Math.PI * 2);

			xVel += xDir;
			yVel += yDir;

		}

		if (moveBackward) {

			move = true;

			xDir = Math.cos((direction % 360) / 360.0 * Math.PI * 2);
			yDir = Math.sin((direction % 360) / 360.0 * Math.PI * 2);

			xVel -= xDir;
			yVel -= yDir;

		}
		if (moveLeft) {

			move = true;

			xDir = Math.cos(((direction - 90) % 360) / 360.0 * Math.PI * 2);
			yDir = Math.sin(((direction - 90) % 360) / 360.0 * Math.PI * 2);

			xVel += xDir;
			yVel += yDir;

		}
		if (moveRight) {

			move = true;

			xDir = Math.cos(((direction + 90) % 360) / 360.0 * Math.PI * 2);
			yDir = Math.sin(((direction + 90) % 360) / 360.0 * Math.PI * 2);

			xVel += xDir;
			yVel += yDir;

		}

		if (!move) {
			return new Point(xPos, yPos);
		}

		gun.recoil += gun.movementRecoil;

		double dist = Math.pow((xVel) * (xVel) + (yVel) * (yVel), 0.5);

		if (dist < 0.01) {
			return new Point(xPos, yPos);
		}

		xVel = xVel * 2 / dist;
		yVel = yVel * 2 / dist;

		return new Point(xPos + xVel, yPos + yVel);

	}

}
